package com.alibaba.algo.components.permission.service.impl;

import com.alibaba.algo.components.permission.domain.SysMenuDomain;
import com.alibaba.algo.components.permission.domain.SysRoleMenuDomain;
import com.alibaba.algo.components.permission.domain.SysUserRoleDomain;
import com.alibaba.algo.components.permission.dto.SysMenuDTO;
import com.alibaba.algo.components.permission.entity.SysMenu;
import com.alibaba.algo.components.permission.entity.SysRoleMenu;
import com.alibaba.algo.components.permission.entity.SysUserRole;
import com.alibaba.algo.components.permission.service.SysMenuService;
import com.alibaba.algo.core.base.Order;
import com.alibaba.algo.core.util.ConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 类SysMenuServiceImpl.java的实现描述：菜单表 serviceimpl
 *
 * @author songwenwei 2020-11-05 16:39:18
 */
@Slf4j
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuDomain sysMenuDomain;
    @Autowired
    private SysUserRoleDomain sysUserRoleDomain;
    @Autowired
    private SysRoleMenuDomain sysRoleMenuDomain;


    @Override
    public SysMenuDTO getMenu(Long userId) {
        LinkedHashMap<String, Object> sortMap = new LinkedHashMap() {{
            put("id", Order.Asc);
        }};
        SysMenuDTO sysMenuDTO = new SysMenuDTO();

        SysUserRole sysUserRoleSearch = new SysUserRole();
        sysUserRoleSearch.setUserId(userId);
        List<SysUserRole> sysUserRoleList = sysUserRoleDomain.listByPara(sysUserRoleSearch, sortMap);
        if (null == sysUserRoleList || sysUserRoleList.size() == 0) {
            return sysMenuDTO;
        }
        Set<Long> roleIdSet = sysUserRoleList.stream().map(p -> p.getRoleId()).collect(Collectors.toSet());


        SysRoleMenu sysRoleMenuSearch = new SysRoleMenu();
        sysRoleMenuSearch.setRoleIdList(roleIdSet);
        List<SysRoleMenu> sysRoleMenuList = sysRoleMenuDomain.listByPara(sysRoleMenuSearch, sortMap);
        if (null == sysRoleMenuList || sysRoleMenuList.size() == 0) {
            return sysMenuDTO;
        }
        Set<Long> menuIdSet = sysRoleMenuList.stream().map(p -> p.getMenuId()).collect(Collectors.toSet());

        SysMenu sysMenuSearch = new SysMenu();
        sysMenuSearch.setIdList(menuIdSet);
        List<SysMenu> sysMenuList = sysMenuDomain.listByPara(sysMenuSearch, new LinkedHashMap() {{
            put("pId", Order.Asc);
            put("sort", Order.Asc);
        }});
        if (null == sysMenuList || sysMenuList.size() == 0) {
            return sysMenuDTO;
        }

        List<SysMenuDTO> sysMenuDTOList = ConvertUtils.copyProperties(SysMenuDTO.class, sysMenuList);

        Map<Long, List<SysMenuDTO>> menuMap = sysMenuDTOList
                .stream()
                .collect(Collectors.groupingBy(SysMenuDTO::getPId));

        fillChildList(-1, menuMap, sysMenuDTO);

        return sysMenuDTO;
    }


    public void fillChildList(long pId, Map<Long, List<SysMenuDTO>> menuMap, SysMenuDTO sysMenuDTO) {
        List<SysMenuDTO> childList = menuMap.get(pId);

        if (null != childList && childList.size() > 0) {
            childList.stream()
                    .forEach(menu -> {
                        fillChildList(menu.getId(), menuMap, menu);
                    });
            sysMenuDTO.setChildList(childList);
        }
    }

}
